package com.fox.hiring.company.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fox.hiring.common.Constant;
import com.fox.hiring.company.dao.ApplicationDao;

/**
 * Servlet implementation class ApplicationServlet
 */
public class ApplicationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private ApplicationDao applicationDao = new ApplicationDao();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 设置POST请求的编码
		request.setCharacterEncoding("UTF-8");
		// 根据命令参数，决定调用哪个操作方法
		String command = request.getParameter("command");
		if ("deleteApplication".equals(command)) {
			deleteApplication(request, response);
		} else if ("?".equals(command)) {

		} else {
			throw new RuntimeException("请求失败！");
		}
	}

	/**
	 * 删除职位申请信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void deleteApplication(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// 1、获取参数
		int applicationId = Integer.parseInt(request
				.getParameter("applicationId"));
		// 2、调用业务逻辑
		int row = applicationDao.deleteApplication(applicationId);
		if (row == 1) {
			response.sendRedirect("TalentServlet?command=main&result="
					+ Constant.OPERATE_SUCCESS);
		} else {
			response.sendRedirect("TalentServlet?command=main&result="
					+ Constant.OPERATE_ERROR);
		}
	}
}
